﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 开单.Model.基础;
using System.Data;
using System.Data.OleDb;
using winform;

namespace 开单.Dao.基础
{
    public class YhxxDao
    {
        YhxxEntity yhxxEntity = new YhxxEntity();
        DataTable dt = new DataTable();
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="YhxxEntity">用户实体类</param>
        public void AddYhxx(YhxxEntity yhxxEntity)
        {
            string sql = string.Empty;
            OleDbParameter[] parameters = {
                     new OleDbParameter("@yh_name", yhxxEntity.yh_name),
                     new OleDbParameter("@yh_psw", yhxxEntity.yh_psw),
                     new OleDbParameter("@yh_kybz", yhxxEntity.yh_kybz),
                     new OleDbParameter("@yh_xm", yhxxEntity.yh_xm),
                     new OleDbParameter("@yh_jp", yhxxEntity.yh_jp)};

            sql = "insert into jc_yhxx(yh_name,yh_psw,yh_kybz,yh_xm,yh_jp) values " +
                    "(@yh_name,@yh_psw,@yh_kybz,@yh_xm,@yh_jp)";

            DbHelperOleDb.ExecuteSql(sql, parameters);
        }

        ///// <summary>
        ///// 删除用户
        ///// </summary>
        ///// <param name="YhxxEntity">用户实体类</param>
        //public void DelYhxx(object yh_code)
        //{
        //    string sql = string.Empty;
        //    OleDbParameter[] parameters = {
        //             new OleDbParameter("@yh_code", yh_code)};

        //    sql = "delete from jc_yhxx where yh_code=@yh_code ";

        //    DbHelperOleDb.ExecuteSql(sql, parameters);
        //}

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public DataTable GetYhxxDt(string yh_jp)
        {
            yh_jp = "%" + yh_jp + "%";
            OleDbParameter[] paramethers ={
                                             new OleDbParameter("@yh_jp",yh_jp)};
            string sql = "select yh_code as 账号编码,yh_xm as 真实姓名,yh_name as 账号,yh_psw as 密码, " +
                " yh_kybz as 可用标志,yh_jp as 简拼 "+
                " from jc_yhxx where yh_jp like @yh_jp ";
            return DbHelperOleDb.ExecuteWithDataTable(sql, paramethers);
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public YhxxEntity GetYhxxToLogin(string yh_name, string yh_psw)
        {
            YhxxEntity yhxxEntity = new YhxxEntity();
            DataTable dt = new DataTable();
            OleDbParameter[] paramethers ={
                                             new OleDbParameter("@yh_name",yh_name),
                                             new OleDbParameter("@yh_psw",yh_psw),                                                               };
            string sql = "select yh_code as 账号编码,yh_xm as 真实姓名,yh_name as 账号,yh_psw as 密码, " +
                " yh_kybz as 可用标志,yh_jp as 简拼 " +
                " from jc_yhxx where yh_name =@yh_name and yh_psw=@yh_psw ";
            dt= DbHelperOleDb.ExecuteWithDataTable(sql, paramethers);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                yhxxEntity.yh_code = Convert.ToInt32(dt.Rows[0][0]);
                yhxxEntity.yh_xm = dt.Rows[0][1].ToString();
                yhxxEntity.yh_name = dt.Rows[0][2].ToString();
                yhxxEntity.yh_psw = dt.Rows[0][3].ToString();
                yhxxEntity.yh_kybz = dt.Rows[0][4].ToString();
                yhxxEntity.yh_jp = dt.Rows[0][5].ToString();
                return yhxxEntity;
            }
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public DataTable GetYhxxDtByYhxm(string yh_xm)
        {
            OleDbParameter[] paramethers ={
                                             new OleDbParameter("@yh_xm",yh_xm)};
            string sql = "select yh_code as 账号编码,yh_name as 账号,yh_psw as 密码, " +
                " yh_kybz as 可用标志,yh_xm as 真实姓名,yh_jp as 简拼 " +
                " from jc_yhxx where yh_xm= @yh_xm ";
            return DbHelperOleDb.ExecuteWithDataTable(sql, paramethers);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public void UpdataYhxx(YhxxEntity yhxxEntity)
        {
            OleDbParameter[] parameters = {
                     new OleDbParameter("@yh_name", yhxxEntity.yh_name),
                     new OleDbParameter("@yh_psw", yhxxEntity.yh_psw),
                     new OleDbParameter("@yh_kybz", yhxxEntity.yh_kybz),
                     new OleDbParameter("@yh_xm", yhxxEntity.yh_xm),
                     new OleDbParameter("@yh_jp", yhxxEntity.yh_jp),
                     new OleDbParameter("@yh_code", yhxxEntity.yh_code)};
            string sql = "update jc_yhxx set yh_name=@yh_name,yh_psw=@yh_psw,yh_kybz=@yh_kybz,"+
                             "yh_xm=@yh_xm,yh_jp=@yh_jp  where yh_code=@yh_code ";
            DbHelperOleDb.ExecuteSql(sql, parameters);
        }

        //public int getInfoCount()
        //{
        //    string sql = "select count(*) from jc_yhxx ";
        //    dt = DbHelperOleDb.ExecuteWithDataTable(sql, null);
        //    int i = Convert.ToInt32(dt.Rows[0][0]);
        //    return i;
        //}
    }
}
